<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" style="width: 100px;"/>
<!--表格区域  -->
    <el-table :data="tableData" style="width: 80%">
      <el-table-column label="URL" prop="url"> </el-table-column>
      <el-table-column label="Name" prop="name"> </el-table-column>
      <el-table-column label="Token" prop="token"> </el-table-column>
      <el-table-column align="right">
        <template slot="header">
          <el-button type="primary" @click="handleCreate" style="width: 200px;">Add</el-button>
        </template>
        <template slot-scope="{row,$index}">
          <el-button size="mini" @click="handleUpdate(row)">Edit</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row,$index)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
<!-- 封装的分页组件 -->
    <pagination v-show="total >0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"></pagination>
<!--弹窗区域  -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFromVisible">
      <el-form :model="temp" ref="dataFrom" :rules="rulse" label-position="left" label-width="70px">
        <el-form-item label="URL" prop="url">
          <el-input v-model="temp.url" placeholder="输入url地址"></el-input>
        </el-form-item>
        <el-form-item label="Name" prop="name">
          <el-input v-model="temp.name" placeholder="输入公司名称"></el-input>
        </el-form-item>
        <el-form-item label="Token" prop="token">
          <el-input v-model="temp.token" placeholder="输入Token"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFromVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogStatus === 'create'?createDate():updateDate()">Confirm</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import Pagination from '@/components/Pagination'
export default {
  name: "Home",
  components: { Pagination },
  data() {
    return {
      total:0,
      listQuery:{
        page:1,
        limit:5,
        title:''
      },
      tableData: [
        {id:1,url: "www.baidu.com",name: "百度",token: "0998KNFDASEA1518"},
        {id:2,url: "www.aiqy",name: "爱奇艺",token: "43q2r4KNKLDK776NAFDA"},
        {id:3,url: "www.tx.com",name: "腾讯视频",token: "000FKLVFOBOVREWV"},
        {id:4,url: "www.youdao.com",name: "有道云笔记",token: "IO54KLNDSADSAINVDW"}, 
        {id:5,url: "www.baidu.com",name: "百度2",token: "0998KNFDASEA1518"},
        {id:6,url: "www.aiqy",name: "爱奇艺2",token: "43q2r4KNKLDK776NAFDA"},
        {id:7,url: "www.tx.com",name: "腾讯视频2",token: "000FKLVFOBOVREWV"},
        {id:8,url: "www.youdao.com",name: "有道云笔记2",token: "IO54KLNDSADSAINVDW"}, 
      ],
      rulse:{
        url:[
          { required:true,message:'输入url地址',trigger:'blur'}
        ],
        name:[
          { required:true,message:'输入公司地址',trigger:'blur'}
        ],
        token:[
          { required:true,message:'输入Token',trigger:'blur'}
        ]
      },
      dialogFromVisible:false,
      dialogStatus:'',
      textMap:{
        update:'Edit-修改',  //修改
        create:'Create-新增' //创建
      },
      temp:{
        id:undefined,
        url:'',
        name:'',
        token:''
      }
    };
  },
  created(){
    this.getList()
  },
  methods: {
    getList(){  //获取整个table数据的接口
      /**
       * fetch(this.listQuery).then((res)=>{
       *  this.total = res.data.totle
       * })
       */

      this.listQuery.page = 2
      this.listQuery.limit = 5
      this.total = this.tableData.length
    },
    resetTemp(){
      this.temp={
        url:'www.',
        name:'',
        token:''
      }
    },
    handleCreate(){
      this.resetTemp()
      this.dialogStatus = 'create',
      this.dialogFromVisible = true,
      this.$nextTick(()=>{
        this.$refs['dataFrom'].clearValidate()
      })
    },
    createDate(){  //新增
      this.$refs['dataFrom'].validate((valid)=>{
        if(valid){
          /**
           * postArticle(this.temp).then(()=>{
           *  this.tableData.unshift(this.temp)
           *  this.dialogFromVisible = false
           * })
           */
          this.tableData.unshift(this.temp)
          this.dialogFromVisible = false
          this.getList()
        }
      })
    },
    handleUpdate(row) {  //修改-回显数据
      this.temp = Object.assign({},row)
      this.dialogStatus = 'update'
      this.dialogFromVisible = true
      this.$nextTick(()=>{
        this.$refs['dataFrom'].clearValidate()
      })
    },
    updateDate(){ //修改-回显数据(完成后)-发送post请求
      this.$refs['dataFrom'].validate((valid)=>{
        if(valid){
          /**
           * const tempDate = Object.assign({},this.temp)
           * putArticle(tempDate).then(()=>{
           *  const index = this.tableData.findIndex((v)=>v.id === this.temp.id)
           *  this.tableData.splice(index,1,this.temp)
           *  this.dialogFromVisible = false
           * })
           */
          const tempDate = Object.assign({},this.temp)
          const index = this.tableData.findIndex((v)=>v.id === this.temp.id)
          this.tableData.splice(index,1,this.temp)
          this.dialogFromVisible = false
        }
      })
    },
    handleDelete(row,index) {  //删除
      // console.log(index, row);
      this.tableData.splice(index,1)
    },
  },
};
</script>
